package routers

import (
	authRouter "git.huoys.com/block_chain/mall/internal/auth/handler"
	authSer "git.huoys.com/block_chain/mall/internal/auth/service"
	shopRouter "git.huoys.com/block_chain/mall/internal/shop/handler"
	shopSer "git.huoys.com/block_chain/mall/internal/shop/service"
	userRouter "git.huoys.com/block_chain/mall/internal/user/handler"
	userSer "git.huoys.com/block_chain/mall/internal/user/service"
	"git.huoys.com/block_chain/mall/server/middleware"
	"github.com/gin-contrib/cors"

	"github.com/gin-gonic/gin"
)

// 初始化router
func InitRouter(sh shopSer.ShopService, us userSer.UserService, au authSer.AuthService) *gin.Engine {
	r := gin.Default()
	// 初始化中间件
	ml := middleware.NewMiddleware(us)
	// 添加中间件
	r.Use(ml.Recovery())
	// todo:本地测试防跨域攻击，上线去掉
	r.Use(cors.New(cors.Config{
		AllowOrigins: []string{"*"},
		AllowMethods: []string{"POST", "GET"},
		AllowHeaders: []string{"content-type", "if-modified-since"},
		//AllowAllOrigins: true,
	}))
	// 在这里添加app路由
	authRouter.Routers(au, r)
	// 后续需要添加token鉴权
	r.Use(ml.Auth())
	shopRouter.Routers(sh, r)
	userRouter.Routers(us, r)
	return r
}
